package com.leoman.spinningmachine.tab1;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.util.FrescoUtil;
import com.leoman.helper.util.LogUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.Enterprise;
import com.leoman.spinningmachine.bean.NewsListBean;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by Administrator on 2018-05-07.
 */
public class NewsActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rl_b)
    RelativeLayout rl_b;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.news_rb1)
    RadioButton news_rb1;
    @BindView(R.id.rv_zcfg)
    RecyclerView rv_zcfg;

    @BindView(R.id.rv_dtgx)
    RecyclerView rv_dtgx;

    @BindView(R.id.head_rl1)
    RelativeLayout head_rl1;
    @BindView(R.id.head_rl2)
    RelativeLayout head_rl2;
    @BindView(R.id.head_rl3)
    RelativeLayout head_rl3;
    @BindView(R.id.tv_notification_title1)
    TextView tv_notification_title1;
    @BindView(R.id.tv_watch_count)
    TextView tv_watch_count;
    @BindView(R.id.tv_date)
    TextView tv_date;

    @BindView(R.id.tzgg_rl)
    RelativeLayout tzgg_rl;
    @BindView(R.id.tzgg_rl2)
    RelativeLayout tzgg_rl2;

    @BindView(R.id.tv_notification_title2)
    TextView tv_notification_title2;
    @BindView(R.id.tv_watch_count2)
    TextView tv_watch_count2;
    @BindView(R.id.tv_date2)
    TextView tv_date2;

    @BindView(R.id.iv_qy)
    SimpleDraweeView iv_qy;
    @BindView(R.id.tv_qy_info)
    TextView tv_qy_info;

    private NewsView1Adapter adapter1;
    private NewsView2Adapter adapter2;
    private Enterprise qyInfobean;
    private List<NewsListBean> noticeList = new ArrayList<>();//通知公告
    private List<NewsListBean> policyList = new ArrayList<>();//政策法规
    private List<NewsListBean> dynamicList = new ArrayList<>();//最新动态

    private List<NewsListBean> tmppolicyList = new ArrayList<>();
    private List<NewsListBean> tmpdynamicList = new ArrayList<>();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_news;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("新闻动态");
        rl_b.setBackgroundResource(R.drawable.ic_news_head_bg);
        ((TextView) head_rl1.findViewById(R.id.item_news_head_tv)).setText("通知公告");
        ((TextView) head_rl2.findViewById(R.id.item_news_head_tv)).setText("政策法规");
        ((TextView) head_rl3.findViewById(R.id.item_news_head_tv)).setText("动态更新");

        rg.setOnCheckedChangeListener(this);
        //政策法规
        adapter1 = new NewsView1Adapter(this, R.layout.item_news_view1, null);
        adapter1.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                gotoFlagOtherActivity(Detail22Activity.class, policyList.get(position).getId());
//                gotoOtherActivity(Detail22Activity.class);
            }
        });
        rv_zcfg.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv_zcfg.setAdapter(adapter1);

        //动态更新
        adapter2 = new NewsView2Adapter(this, R.layout.item_hot, null);
        adapter2.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                gotoFlagOtherActivity(Detail22Activity.class, tmpdynamicList.get(position).getId());
            }
        });
        rv_dtgx.setLayoutManager(new LinearLayoutManager(this));
        rv_dtgx.setAdapter(adapter2);
    }

    @Override
    protected void initData() {
        WebServiceApi.getInstance().newsQueryList("", "", "", this, 1);
        WebServiceApi.getInstance().newsQueryEnterpriseInfo(this, 2);
    }

    @OnClick({R.id.head_rl1, R.id.head_rl2, R.id.head_rl3})
    public void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.head_rl1:
                gotoFlagDataOtherActivity(TzggActivity.class, noticeList, 1);
                break;
            case R.id.head_rl2:
                gotoFlagDataOtherActivity(DtUpdateActivity.class, policyList, 2);
                break;
            case R.id.head_rl3:
                gotoFlagDataOtherActivity(DtUpdateActivity.class, dynamicList, 3);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.news_rb1:
                if (qyInfobean != null) {
                    LogUtil.json("1111:" + qyInfobean.getEnterpriseIntroduce());
                    tv_qy_info.setText("\u3000\u3000" + qyInfobean.getEnterpriseIntroduce());
                    FrescoUtil.setImg(this, iv_qy, qyInfobean.getEnterpriseImage());

                }
                break;
            case R.id.news_rb2:
                if (qyInfobean != null) {
                    LogUtil.json("2222:" + qyInfobean.getParkZone());
                    tv_qy_info.setText("\u3000\u3000" + qyInfobean.getParkZone());
                    FrescoUtil.setImg(this, iv_qy, qyInfobean.getParkImage());
                }
                break;
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        if (1 == tag) {
            List<NewsListBean> lists = apiResponse.data.list;
            if (lists != null && lists.size() > 0) {
                for (int i = 0; i < lists.size(); i++) {
                    NewsListBean bean = lists.get(i);
                    if (1 == bean.getType()) {
                        noticeList.add(bean);
                    } else if (2 == bean.getType()) {
                        policyList.add(bean);
                    } else if (3 == bean.getType()) {
                        dynamicList.add(bean);
                    }
                }
                setNoticeData(noticeList);


                if (policyList != null && policyList.size() > 5) {
                    tmppolicyList = policyList.subList(0, 5);
                } else {
                    tmppolicyList = policyList;
                }
                adapter1.setData(policyList);

                if (dynamicList != null && dynamicList.size() > 5) {
                    tmpdynamicList = dynamicList.subList(0, 5);
                } else {
                    tmpdynamicList = dynamicList;
                }
                adapter2.setData(tmpdynamicList);
            }
        } else if (2 == tag) {
            qyInfobean = apiResponse.data.enterprise;
            if (qyInfobean != null) {
                tv_qy_info.setText("\u3000\u3000" + qyInfobean.getEnterpriseIntroduce());
                FrescoUtil.setImg(this, iv_qy, qyInfobean.getEnterpriseImage());
            }
        }
    }

    /**
     * 通知公告
     *
     * @param noticeList
     */
    private void setNoticeData(List<NewsListBean> noticeList) {
        if (noticeList != null && noticeList.size() > 0) {
            if (noticeList.size() >= 1) {
                final NewsListBean bean = noticeList.get(0);
                tv_notification_title1.setText("" + bean.getTitle());
                tv_watch_count.setText("" + bean.getViewNum());
                tv_date.setText("" + DateUtil.getyMdStr(bean.getCreateDate()));
                tzgg_rl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gotoFlagOtherActivity(Detail22Activity.class, bean.getId());
                    }
                });
            }
            if (noticeList.size() >= 2) {
                 NewsListBean bean = noticeList.get(1);
                tv_notification_title2.setText("" + bean.getTitle());
                tv_watch_count2.setText("" + bean.getViewNum());
                tv_date2.setText("" + DateUtil.getyMdStr(bean.getCreateDate()));
                tzgg_rl2.setTag(R.id.text1,bean);
                tzgg_rl2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        NewsListBean bean = (NewsListBean)v.getTag(R.id.text1);
                        gotoFlagOtherActivity(Detail22Activity.class, bean.getId());
                    }
                });
            }
        }
    }
}
